using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMRelationshipSingleToMultipleCannotBePKorUC : IBusinessObjectRule
    {
        private DMRelationship _dmRelationship;

//        public DMRelationshipSingleToMultipleCannotBePKorUC(DMRelationship dmRelationship)
//        {
//            if (dmRelationship == null) throw new ArgumentNullException("dmRelationship");
//            _dmRelationship = dmRelationship;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmRelationship = (DMRelationship) bo;
            if (_dmRelationship.ClassDM == null) return true;
            DMRelationship reverseRelationship = (DMRelationship)_dmRelationship.ReverseRelationship;
            if (reverseRelationship == null) return true;
            if (reverseRelationship.ClassDM == null) return true;
            if (_dmRelationship.Cardinality == Cardinality.Single && reverseRelationship.Cardinality == Cardinality.Multiple)
            {
                return (!(_dmRelationship.HasAllPkProps() || _dmRelationship.HasAllAkProps()));
            }
            return true;
        }

        public string Name
        {
            get { return "DMRelationshipSingleToMultiplePropKey"; }
        }

        public string Message
        {
            get
            {
                return "The single side of the One To Many Relationship '" + _dmRelationship +
                       "' on Class '" + _dmRelationship.ClassDM + "' should not be mapped to a Primary Key or Alternate Key. " 
                       + "This should possibly be a One To One Relationship.";
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}